package com.example.webservice.dao.PojoDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.example.webservice.Cliente;
import com.example.webservice.dao.OracleGenericDAO;
import com.example.webservice.dao.Interface.ClienteDAO;

public class OrcClienteDAO extends OracleGenericDAO implements ClienteDAO {

	@Override
	public List<Cliente> getClienteByPromotor(int codUsur) {
		Connection connection = null;
	     PreparedStatement pstmt = null;
	     ResultSet rs = null;  
	     List<Cliente> clientes = new ArrayList<>();
	    try{
		connection = getConnection();
		String query = "select codcli,cliente,cgcent,ieent,codusur1 from pcclient c where dtexclusao is null and codusur1 in (471,168,1102)";
        
        String sql = "select codcli,cliente,cgcent,ieent,codusur1 " +
              "FROM pcclient " +
              "WHERE dtexclusao is null and codusur1 = ? ";
        pstmt = connection.prepareStatement(sql);
        pstmt.setInt(1, codUsur);
        
        rs = pstmt.executeQuery();

      
        	while (rs.next()) {
				Cliente cliente = new Cliente();
				cliente.setCodigo(rs.getLong("codcli"));
				cliente.setNome(rs.getString("cliente"));
				cliente.setCnpj(rs.getString("cgcent"));
				cliente.setCodusur(rs.getInt("codusur1"));
				cliente.setIe(rs.getString("ieent"));
				clientes.add(cliente);
			}
           return clientes;        
	    } catch (Exception e) {
	         e.printStackTrace();
	         return null;
	      } finally {
	         close(rs);
	         close(pstmt);
	         close(connection);
	      }
	
	}
	@Override
	public Cliente getCliente(int codigo) {
		Connection connection = null;
	     PreparedStatement pstmt = null;
	     ResultSet rs = null;  
	     List<Cliente> clientes = new ArrayList<>();
	    try{
		connection = getConnection();
		String query = "select codcli,cliente,cgcent,ieent,codusur1 from pcclient c where dtexclusao is null and codusur1 in (471,168,1102)";
       
       String sql = "select codcli,cliente,cgcent,ieent,codusur1 " +
             "FROM pcclient " +
             "WHERE dtexclusao is null and codcli = ? ";
       pstmt = connection.prepareStatement(sql);
       pstmt.setInt(1, codigo);
       
       rs = pstmt.executeQuery();

     
       if (rs.next()) {
				Cliente cliente = new Cliente();
				cliente.setCodigo(rs.getLong("codcli"));
				cliente.setNome(rs.getString("cliente"));
				cliente.setCnpj(rs.getString("cgcent"));
				cliente.setCodusur(rs.getInt("codusur1"));
				cliente.setIe(rs.getString("ieent"));
			return cliente;
       } else {
          return null;
       }       
	    } catch (Exception e) {
	         e.printStackTrace();
	         return null;
	      } finally {
	         close(rs);
	         close(pstmt);
	         close(connection);
	      }
	
	}

}
